home *** CD-ROM | disk | FTP | other *** search
- Path: nntpserver.sp.ac.sg.!usenet
- From: ABC <s7700038@singnet.com.sg>
- Newsgroups: comp.lang.c
- Subject: Help on display names
- Date: 25 Mar 1996 08:49:45 GMT
- Organization: Singapore Polytechnic
- Message-ID: <4j5mn9$mn2@scctn01.sp.ac.sg>
- NNTP-Posting-Host: 164.78.196.219
- Mime-Version: 1.0
- Content-Type: multipart/mixed;
- boundary="-------------------------------22081592413785"
- X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
-
- This is a multi-part message in MIME format.
-
- ---------------------------------22081592413785
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
-
- I need to display a list of names before and after my ADD function
- I managed to display a list of names, compute my function, but when i
- display the names again, my display function will cock up (display NULL).
- Can anyone please give some guidance on this subject?
- Thanks
-
-
- ---------------------------------22081592413785
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain
-
- #include<stdio.h>
- #include<alloc.h>
- #include<conio.h>
- #include<string.h>
- #include<ctype.h>
- #include<stdlib.h>
- #define NSIZE 30 //--array of size to hold name
- #define ASIZE 100 //--array of size to hold address
- #define FILENAME "B:DATA.DAT"
-
- typedef struct data
- {
- char name[NSIZE];
- char address[ASIZE];
- int age;
- struct data *next;
- }NodeItem;
- typedef NodeItem *Nodeptr;
-
- void menu(void);
- void load_record(FILE *fp, Nodeptr *headptr, Nodeptr *lastptr);
- void create_link_list(Nodeptr *headptr, Nodeptr *lastptr);
- void display_record(Nodeptr *headptr);
- void save_record(FILE *fp, Nodeptr *headptr);
-
- int main()
- {
- Nodeptr headptr,lastptr;
- char ans1;
- char choice;
-
- FILE *fp;
- headptr=NULL;
- load_record(fp, &headptr, &lastptr);
- do
- {
- menu();
- choice=getchar();
- choice=toupper(choice);
- flushall();
- switch(choice)
- {
- case 'A': flushall();
- clrscr();
- display_record(&headptr);
- create_link_list(&headptr, &lastptr);
- break;
- case 'B': flushall();
- save_record(fp, &headptr);
- break;
- case 'C': exit(1);
- default : break;
- }
- }while(choice!='C');
- return 0;
- }
-
- void menu(void)
- {
- printf("\n\nA) ENTER PARTICULAR(S)\n");
- printf("B) SAVE\n");
- printf("C) EXIT\n");
- printf("\nEnter your choice : ");
- return;
- }
-
- void create_link_list(Nodeptr *headptr, Nodeptr *lastptr)
- {
- int node_size=sizeof(NodeItem);
- Nodeptr ptr;
- char answer='N';
-
- do
- {
- ptr=(Nodeptr)malloc(node_size);
- printf("\n\nÜÜÜÜÜÜÜÜÜÜ ENTER PARTICULAR(S) ÜÜÜÜÜÜÜÜÜÜ\n");
- printf("\nPlease enter name : ");
- gets(ptr->name);
- if(ptr->name[0]=='0')
- {
- return;
- }
- while(ptr->name[0]=='\0')
- {
- printf("\nPlease enter name : ");
- gets(ptr->name);
- }
- strcpy(ptr->name, strupr(ptr->name));
- flushall();
- printf("\nPlease enter address : ");
- gets(ptr->address);
- while(ptr->address[0]=='\0')
- {
- printf("\nPlease enter address:");
- gets(ptr->address);
- }
- strcpy(ptr->address,strupr(ptr->address));
- flushall();
- printf("\nPlease enter age : years old\b\b\b\b\b\b\b\b\b\b\b\b");
- scanf("%d", &(ptr->age));
- while(ptr->age< 1 || ptr->age>100) //--check for invalid age
- {
- printf("\nPlease enter age : years old\b\b\b\b\b\b\b\b\b\b\b\b");
- scanf("%d", &(ptr->age));
- }
- flushall();
- if((*headptr)==NULL)
- {
- (*headptr)=ptr;
- (*lastptr)=ptr;
- }
- else
- {
- (*lastptr)->next=ptr;
- (*lastptr)=ptr;
- }
- printf("\nAny more? <Y/N> : ");
- answer=getchar();
- answer=toupper(answer);
- flushall();
- }while(answer!='N');
- (*lastptr)->next=NULL;
- return;
- }
-
- void display_record(Nodeptr *headptr)
- {
- Nodeptr ptr;
- int y;
-
- ptr=(*headptr);
- while(ptr!=NULL)
- {
- for(y=1;y<10;y++)
- {
- gotoxy(1,y);printf("%s", ptr->name);
- ptr=ptr->next;
- }
- for(y=1;y<10;y++)
- {
- gotoxy(25,y);printf("%s", ptr->name);
- ptr=ptr->next;
- }
- for(y=1;y<10;y++)
- {
- gotoxy(50,y);printf("%s", ptr->name);
- ptr=ptr->next;
- }
- }
- return;
- }
-
- void load_record(FILE *fp, Nodeptr *headptr, Nodeptr *lastptr)
- {
- Nodeptr load;
- int node_size=sizeof(NodeItem);
- int name_length,address_length;
-
- clrscr();
- if((fp=fopen(FILENAME,"r"))==NULL)
- {
- printf("\a\n\nERROR!!!!FILE CAN'T BE OPEN!!!\n");
- }
- else
- {
- while(!feof(fp))
- {
- load=(Nodeptr)malloc(node_size);
- fscanf(fp, "%d\n", &name_length);
- fscanf(fp, "%d\n", &address_length);
- fgets(load->name,name_length+1, fp);
- fscanf(fp,"\n");
- fgets(load->address, address_length+1, fp);
- fscanf(fp, "%d\n", &load->age);
-
- if((*headptr)==NULL)
- {
- (*headptr)=load;
- (*lastptr)=load;
- }
- else
- {
- (*lastptr)->next=load;
- (*lastptr)=load;
- }
- }
- (*lastptr)->next=NULL;
- }
- fclose(fp);
- return;
- }
-
- void save_record(FILE *fp, Nodeptr *headptr)
- {
- Nodeptr save;
- int i;
-
- if ((fp=fopen(FILENAME,"w"))==NULL)
- {
- printf("\a\n\nERROR!!!!FILE CAN'T BE OPEN!!!\n");
- }
- else
- {
- save=(*headptr); /****/
-
- while(save!=NULL)
- {
- fprintf( fp,"%d\n", strlen(save->name));
- fprintf( fp, "%d\n", strlen(save->address));
- fprintf( fp, "%s\n", save->name);
- fprintf( fp, "%s\n", save->address);
- fprintf( fp, "%d\n", save->age);
- save=save->next;
- }
- }
- fclose(fp);
- printf("\a\n\nSAVING DONE!!!!!\n");
- printf("\nPress any key to continue.....");
- getch();
- return;
- }
- ---------------------------------22081592413785--
-